/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-08-20 16:36:59
 * @LastEditTime: 2025-09-08 16:51:45
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\app\sensor_task.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_err.h"
#include "qmi8658.h"
#include "internal_temp_sensor.h"
#include "sensor_task.h"

static const char* TAG = "sensor_task";

extern i2c_obj_t i2c0_master;

float temp;
qmi8658_t qmi8658;

/**
 * @description: 获取芯片内部温度
 * @return {*}
 */
float get_chip_temperature(void)
{
    return temp;
}

/**
 * @description: 传感器数据获取任务
 * @param {void*} arg
 * @return {*}
 */
void sensor_task(void* arg)
{
    temperature_sensor_init();
    qmi8658_init(i2c0_master);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    while(1)
    {
        vTaskDelay(1000 / portTICK_PERIOD_MS);

        temp = sensor_get_temperature();     
        qmi8658_fetch_angleFromAcc(&qmi8658);

        ESP_LOGI(TAG, "chip temperature = %.1f (°C)",temp);
        ESP_LOGI(TAG, "angle_x = %.1f  angle_y = %.1f angle_z = %.1f",qmi8658.AngleX, qmi8658.AngleY, qmi8658.AngleZ);
    }
}

void sensor_task_init(void)
{
    xTaskCreate(sensor_task, "sensor", 4096, NULL, 3, NULL);
    //xTaskCreatePinnedToCore(sensor_task, "sensor_task", 4096, NULL, 3, NULL, 1);
}